package com.cybersapien.comng.engine.event.engine;

import com.cybersapien.comng.engine.context.ContextHelper;
import com.cybersapien.comng.engine.type.IStack;

public class EventManager {

	/* ====== SINGLETON MANAGEMENT ===== */

	static private EventManager instance = null;

	static public EventManager getInstance() {
		if (instance == null) {
			initInstance();
		}
		return instance;
	}

	static private synchronized void initInstance() {
		if (instance == null) {
			instance = new EventManager();
		}
	}

	/* ===== */

	/**
	 * 
	 */
	private EventManager() {

	}

	public void stackEventEngine(IEventEngine event) {
		ContextHelper.addEventEngine(event);
	}

	public void fireStackEventEngine() {

		while (ContextHelper.hasNextEventEngine()) {
			IEventEngine event = ContextHelper.nextEventEngine();

			IStack<IEventEngineListerner> listeners = EventListenerManager.getInstance().findListeners(event);

			for (IEventEngineListerner listener : listeners) {
				listener.undergoEvent(event);
			}

		}

	}

	public void fireEventEngine(IEventEngine event) {
		stackEventEngine(event);
		fireStackEventEngine();
	}

}
